package com.movie.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.movie.beans.*;
import com.movie.beans.dto.MoviesDto;
import com.movie.service.MovieService;
import com.movie.service.SiteRateService;
import com.movie.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class MovieIndexController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private SiteRateService siteRateService;

    @GetMapping("/")
    public String index(ModelMap modelMap){
        List<Movies> movies = movieService.selectNewest10List();
        List<MoviesDto> new10dtos = new ArrayList<>();
        for (Movies movie:movies) {
            SiteRate siteRate = siteRateService.selectSiteRateByMovieId(movie.getMovieId());
            MoviesDto dto = new MoviesDto();
            if(siteRate!=null){
                dto.setMovieId(movie.getMovieId());
                dto.setTitle(movie.getTitle());
                dto.setPoint(siteRate.getPoint());
                new10dtos.add(dto);
            }
            else {
                dto.setMovieId(movie.getMovieId());
                dto.setTitle(movie.getTitle());
                dto.setPoint(0.0);
                new10dtos.add(dto);
            }
        }
//        List<Movies> rateMovies = movieService.selectTop10RateList();
        modelMap.addAttribute("new10",new10dtos);
//        modelMap.addAttribute("rate10",rateMovies);
        return "/movie";
    }

    @GetMapping("/default")
    @ResponseBody
    public List<MoviesDto> Default(){
        List<SiteRate> list = siteRateService.getDefaultMovies();
        List<MoviesDto> dtoList = new ArrayList<>();
        for (SiteRate item: list) {
            MoviesDto dto = new MoviesDto();
            dto.setActor("John/Rose");
            dto.setDirector("Merry");
            dto.setGenres(movieService.getMoviesById(item.getMovieId()).getGenres());
            dto.setMovieId(item.getMovieId());
            dto.setTitle(movieService.getMoviesById(item.getMovieId()).getTitle());
            dto.setPoint(item.getPoint());
            dto.setSiteRate((int) (item.getRate()*10));
            dtoList.add(dto);
        }
        return dtoList;
    }

    @GetMapping("/recommend")
    @ResponseBody
    public List<MoviesDto> recommend(){
        SysUser u = ShiroUtil.getSysUser();
        Recommend recommend = movieService.selectRecommend(u.getUserId());
        //推荐表中不存在此用户，则为其推荐默认电影列表
        if (recommend==null){
            List<SiteRate> list = siteRateService.getDefaultMovies();
            List<MoviesDto> dtoList = new ArrayList<>();
            for (SiteRate item: list) {
                MoviesDto dto = new MoviesDto();
                dto.setActor("John/Rose");
                dto.setDirector("Merry");
                dto.setGenres(movieService.getMoviesById(item.getMovieId()).getGenres());
                dto.setMovieId(item.getMovieId());
                dto.setTitle(movieService.getMoviesById(item.getMovieId()).getTitle());
                dto.setPoint(item.getPoint());
                dto.setSiteRate((int) (item.getRate()*10));
                dtoList.add(dto);
            }
            return dtoList;
        }
        Movies m1= movieService.getMoviesById(recommend.getMovie1Id());
        Movies m2= movieService.getMoviesById(recommend.getMovie2Id());
        Movies m3= movieService.getMoviesById(recommend.getMovie3Id());
        Movies m4= movieService.getMoviesById(recommend.getMovie4Id());
        Movies m5= movieService.getMoviesById(recommend.getMovie5Id());
        List<Movies> list = new ArrayList<>();
        if(m1!=null){
            list.add(m1);
        }
        if(m2!=null){
            list.add(m2);
        }
        if(m3!=null){
            list.add(m3);
        }
        if(m4!=null){
            list.add(m4);
        }
        if(m5!=null){
            list.add(m5);
        }
        List<MoviesDto> dtoList = new ArrayList<>();
        for (Movies item: list) {
            MoviesDto dto = new MoviesDto();
            dto.setActor("John/Rose");
            dto.setDirector("Merry");
            dto.setGenres(item.getGenres());
            dto.setMovieId(item.getMovieId());
            dto.setTitle(item.getTitle());
            SiteRate siteRate = siteRateService.selectSiteRateByMovieId(item.getMovieId());
            if(siteRate!=null){
                dto.setPoint(siteRate.getPoint());
                dto.setSiteRate((int) (siteRate.getRate()*10));
            }
            else {
                dto.setPoint(0.0);
                dto.setSiteRate(0);
            }
            dtoList.add(dto);
        }
        return dtoList;
    }



    @GetMapping("/search")
    public String search(int limit,int page, @RequestParam String searchVal,ModelMap modelMap){
        System.out.println(limit);
        System.out.println(page);
        PageHelper.startPage(page,limit);
        List<Movies> movies= movieService.getMoviesBySearchVal(searchVal);
        List<MoviesDto> dtos = new ArrayList<>();
        for (Movies movie:movies) {
            SiteRate siteRate = siteRateService.selectSiteRateByMovieId(movie.getMovieId());
            MoviesDto dto = new MoviesDto();
            dto.setActor("John/Rose");
            dto.setDirector("Merry");
            dto.setGenres(movie.getGenres());
            dto.setMovieId(movie.getMovieId());
            dto.setTitle(movie.getTitle());
            if(siteRate!=null){
                dto.setPoint(siteRate.getPoint());
                dto.setSiteRate((int) (siteRate.getRate()*10));
            }
            else {
                dto.setPoint(0.0);
                dto.setSiteRate((int) 0.0);
            }

            dtos.add(dto);
        }
        modelMap.addAttribute("count", new PageInfo(movies).getTotal());
        modelMap.addAttribute("searchVal",searchVal);
        modelMap.addAttribute("curr",page);
        modelMap.addAttribute("movies",dtos);
        return "/page/search";
    }


}
